/*
 * Copyright 1999-2010 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.alibaba.aone.casestudy.common;

import com.alibaba.intl.commons.framework.dto.BaseQuery;

/**
 * 类BaseCondition.java的实现描述： 为casestudy的多条件查询提供分页
 * 
 * @author gaojian 2011-4-28 下午04:09:42
 */
public class BaseCondition extends BaseQuery {

    private static final long serialVersionUID = -7663861189339747540L;
    private static final int PAGE_SIZE = 20;

    /**
     * 默认pagesize为20
     */
    public BaseCondition() {
        setPageSize(PAGE_SIZE);
    }

    /**
     * 按指定的pagesize分页<br>
     * <li>page size 必须大于5</li>
     * 
     * @param pageSize
     * @throws IllegalArgumentException 如果pagesize为空或小于5
     */
    public BaseCondition(Integer pageSize) {
        if (pageSize == null || pageSize < 5)
            throw new IllegalArgumentException("page size must greater than 4");
        setPageSize(pageSize);
    }
    

    /* (non-Javadoc)
     * @see com.alibaba.intl.commons.framework.dto.BaseQuery#buildStartEndRow(int, int)
     */
    public void buildStartEndRow(int startRow, int rows) {
        //为mysql的分页重写计算开始结束行的代码
        startRow--;
        if (startRow < 0) {
            startRow = 0;
        }
        super.setStartRow(startRow);
        super.setEndRow(startRow);
        if (rows > 0) {
            super.setEndRow((super.getEndRow() + rows) - 1);
        }
    }
}
